package com.dragon.app.ykd.service;

import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.dragon.app.commons.vo.JsonResult;
import com.dragon.app.marvel2.NewMarvelCoreService;
import com.dragon.app.ykd.dto.AcLoanApplyNode;
import com.dragon.loan.service.LoanInfoIDragonService;
import com.fintech.common.entity.ResultData;
import com.fintech.common.enums.ReturnCode;
import com.fintech.common.util.RedisUtils;
import com.fintech.user.entity.RealNameAuthentication;
import com.fintech.user.entity.User;
import com.fintech.user.service.RealNameAuthenticationService;
import com.marvels.common.utils.MarvelsLogUtil;
import com.marvels.financial.loan.service.ReimbursementProcessService;

/**
 * 个人用户中心
 * @author liuxy
 *
 */
@Service("appUserAppService")
public class AppUserServiceImpl implements AppUserService{
	@Autowired
	private LoanInfoIDragonService loanInfoIDragonService;
	@Autowired
	private RealNameAuthenticationService realNameAuthenticationService;
	@Autowired
	private NewMarvelCoreService newMarvelCoreService;
	@Autowired
	private AcLoanApplyNodeService acLoanApplyNodeService;
	@Autowired
	private ReimbursementProcessService reimbursementProcessService;
	/**
	 * 根据loanid查看借据
	 * @param loanId
	 * @return
	 */
	@Override
	public Object queryDebt(String loanId){
		JsonResult<Map<String,Object>> jsonResult = new JsonResult<Map<String,Object>>();
		try {
			// 判断是否走新核心系统接口
			boolean marvelStart = newMarvelCoreService.isMarvelStart(null, loanId);
			if(marvelStart){
				com.marvels.financial.loan.dto.RepaymentNoMobileDTO repaymentNoMobileDTO = new com.marvels.financial.loan.dto.RepaymentNoMobileDTO();
				repaymentNoMobileDTO.setLoanId(loanId);
				Map data = reimbursementProcessService.queryDebt(repaymentNoMobileDTO);
				jsonResult.setMessage("成功");
				jsonResult.setData(data);
				return jsonResult;
				/*LoanIdDTO loanIdDTO = new LoanIdDTO();
				loanIdDTO.setLoanId(loanId);
				return newMarvelCoreService.getForObjectPost(MarvelURLConstant.FINANCIAL_PROCESS_API_QUERYDEBT, loanIdDTO, Map.class);*/
			}
			Map<String,Object> debtMap = loanInfoIDragonService.queryDebt(loanId);
			jsonResult.setData(debtMap);
		} catch (Exception e) {
			jsonResult.setErrorCode(-2);
			jsonResult.setErrorMsg("查询失败!");
			MarvelsLogUtil.getInstance().error("=====queryDebt/{loanId}=====", e);
		}
		return jsonResult;
	}
	
	/**
	 * 查询借款记录
	 * @param accessToken
	 * @return
	 */
	@Override
	public JsonResult<List<Map<String,String>>> borrowList(String accessToken, String idFintechUmUser){
		JsonResult<List<Map<String,String>>> jsonResult = new JsonResult<List<Map<String,String>>>(); 
		ResultData<?> resultData= RedisUtils.verifyUser(accessToken, idFintechUmUser,User.class);
		if(!resultData.getResultCode().equals(ReturnCode.success.getErrorCode())){
			jsonResult.setErrorMsg(Integer.valueOf(resultData.getResultCode()),resultData.getMessage());
			return jsonResult;
		}
		User user=(User)resultData.getData();
		
		try {
			String userInfo=user.getMobileNo();
			RealNameAuthentication realNameAuthentication = realNameAuthenticationService.queryByUserId(user.getId());
			if(realNameAuthentication!=null){
				userInfo=realNameAuthentication.getIdentityCardNo();
			}
			List<Map<String,String>> listMap = loanInfoIDragonService.borrowList(userInfo);
			for (Map<String, String> map : listMap) {
				String status=map.get("status");
				if("A2".equalsIgnoreCase(status)||"A3".equalsIgnoreCase(status)){
					map.put("statusZh", "已还清");
				}else{
					map.put("statusZh", "未还清");
				}
				// 查询节点信息
				AcLoanApplyNode acLoanApplyNode = acLoanApplyNodeService.queryLastApplyNode(map.get("loan_id"));
				if (acLoanApplyNode != null) {
					map.put("nodeCode", acLoanApplyNode.getNodeCode() + "");
				}
			}
			jsonResult.setData(listMap);
		} catch (Exception e) {
			jsonResult.setErrorMsg("系统异常!");
			MarvelsLogUtil.getInstance().error("=====borrowList=====", e);
		}
		return jsonResult;
	}

}
